import { ref } from 'vue';
import { getMessageStore } from '@/store/message/utils';
import { IMessagePreview } from '../types';

export interface IContextmenuData {
  title: string;
  action: any;
}
export interface IContextmenuConfig {
  show: boolean;
  x: number;
  y: number;
  menus: IContextmenuData[];
  data?: IMessagePreview;
}
export const contextmenuConfig = ref<IContextmenuConfig>({
  show: false,
  x: 0,
  y: 0,
  menus: [
    {
      title: '标为已读',
      action() {
        if (contextmenuConfig.value.data) {
          contextmenuConfig.value.data.unread =
            !contextmenuConfig.value.data.unread;
        }
      },
    },
    {
      title: '置顶该聊天',
      action() {
        if (contextmenuConfig.value.data) {
          if (contextmenuConfig.value.data.topping) {
            contextmenuConfig.value.data.sort += 1000;
          } else {
            contextmenuConfig.value.data.sort -= 1000;
          }
          contextmenuConfig.value.data.topping =
            !contextmenuConfig.value.data.topping;
        }
      },
    },
    {
      title: '不显示该聊天',
      action() {
        if (contextmenuConfig.value.data) {
          contextmenuConfig.value.data.show = false;
        }
      },
    },
    {
      title: '删除该聊天',
      action() {
        const store = getMessageStore();
        store.list = store.list.filter((e) => {
          return e !== contextmenuConfig.value.data;
        });
      },
    },
  ],
});
